<?php
namespace Leaps\Database\Eloquent\Relations;

use Leaps\Database\Eloquent\Model;
use Leaps\Database\Eloquent\Builder;
use Leaps\Database\Eloquent\Collection;

abstract class HasOneOrMany extends Relation {

	/**
	 * The foreign key of the parent model.
	 *
	 * @var string
	 */
	protected $foreignKey;

	/**
	 * Create a new has many relationship instance.
	 *
	 * @param  \Leaps\Database\Eloquent\Builder  $query
	 * @param  \Leaps\Database\Eloquent\Model  $parent
	 * @param  string  $foreignKey
	 * @return void
	 */
	public function __construct(Builder $query, Model $parent, $foreignKey)
	{
		$this->foreignKey = $foreignKey;
		parent::__construct($query, $parent);
	}

	/**
	 * Set the base constraints on the relation query.
	 *
	 * @return void
	 */
	public function addConstraints()
	{
		$key = $this->parent->getKey();
		$this->query->where($this->foreignKey, '=', $key);
	}

	/**
	 * Set the constraints for an eager load of the relation.
	 *
	 * @param  array  $models
	 * @return void
	 */
	public function addEagerConstraints(array $models)
	{
		$this->query->whereIn($this->foreignKey, $this->getKeys($models));
	}

	/**
	 * Match the eagerly loaded results to their single parents.
	 *
	 * @param  array   $models
	 * @param  \Leaps\Database\Eloquent\Collection  $results
	 * @param  string  $relation
	 * @return array
	 */
	public function matchOne(array $models, Collection $results, $relation)
	{
		return $this->matchOneOrMany($models, $results, $relation, 'one');
	}

	/**
	 * Match the eagerly loaded results to their many parents.
	 *
	 * @param  array   $models
	 * @param  \Leaps\Database\Eloquent\Collection  $results
	 * @param  string  $relation
	 * @return array
	 */
	public function matchMany(array $models, Collection $results, $relation)
	{
		return $this->matchOneOrMany($models, $results, $relation, 'many');
	}

	/**
	 * Match the eagerly loaded results to their many parents.
	 *
	 * @param  array   $models
	 * @param  \Leaps\Database\Eloquent\Collection  $results
	 * @param  string  $relation
	 * @param  string  $type
	 * @return array
	 */
	protected function matchOneOrMany(array $models, Collection $results, $relation, $type)
	{
		$dictionary = $this->buildDictionary($results);
		foreach ($models as $model)
		{
			$key = $model->getKey();
			if (isset($dictionary[$key]))
			{
				$value = $this->getRelationValue($dictionary, $key, $type);
				$model->setRelation($relation, $value);
			}
		}
		return $models;
	}

	/**
	 * Get the value of a relationship by one or many type.
	 *
	 * @param  array   $dictionary
	 * @param  string  $key
	 * @param  string  $type
	 * @return mixed
	 */
	protected function getRelationValue(array $dictionary, $key, $type)
	{
		$value = $dictionary[$key];
		return $type == 'one' ? reset($value) : $this->related->newCollection($value);
	}

	/**
	 * Build model dictionary keyed by the relation's foreign key.
	 *
	 * @param  \Leaps\Database\Eloquent\Collection  $results
	 * @return array
	 */
	protected function buildDictionary(Collection $results)
	{
		$dictionary = array();
		$foreign = $this->getPlainForeignKey();
		foreach ($results as $result)
		{
			$dictionary[$result->{$foreign}][] = $result;
		}
		return $dictionary;
	}

	/**
	 * Attach a model instance to the parent model.
	 *
	 * @param  \Leaps\Database\Eloquent\Model  $model
	 * @return \Leaps\Database\Eloquent\Model
	 */
	public function save(Model $model)
	{
		$model->setAttribute($this->getPlainForeignKey(), $this->parent->getKey());
		return $model->save() ? $model : false;
	}

	/**
	 * Attach an array of models to the parent instance.
	 *
	 * @param  array  $models
	 * @return array
	 */
	public function saveMany(array $models)
	{
		array_walk($models, array($this, 'save'));
		return $models;
	}

	/**
	 * Create a new instance of the related model.
	 *
	 * @param  array  $attributes
	 * @return \Leaps\Database\Eloquent\Model
	 */
	public function create(array $attributes)
	{
		$foreign = array(
			$this->getPlainForeignKey() => $this->parent->getKey()
		);
		$instance = $this->related->newInstance();
		$instance->setRawAttributes(array_merge($attributes, $foreign));
		$instance->save();
		return $instance;
	}

	/**
	 * Create an array of new instances of the related model.
	 *
	 * @param  array  $records
	 * @return array
	 */
	public function createMany(array $records)
	{
		$instances = array();
		foreach ($records as $record)
		{
			$instances[] = $this->create($record);
		}
		return $instances;
	}

	/**
	 * Perform an update on all the related models.
	 *
	 * @param  array  $attributes
	 * @return int
	 */
	public function update(array $attributes)
	{
		if ($this->related->usesTimestamps())
		{
			$attributes[$this->relatedUpdatedAt()] = $this->related->freshTimestamp();
		}
		return $this->query->update($attributes);
	}

	/**
	 * Get the foreign key for the relationship.
	 *
	 * @return string
	 */
	public function getForeignKey()
	{
		return $this->foreignKey;
	}

	/**
	 * Get the plain foreign key.
	 *
	 * @return string
	 */
	public function getPlainForeignKey()
	{
		$segments = explode('.', $this->getForeignKey());
		return $segments[count($segments) - 1];
	}
}